package com.example.newinfinity.DownloadImage

import android.animation.Animator
import android.content.Context
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout.LayoutParams
import android.widget.PopupWindow
import com.example.newinfinity.databinding.DownLoadAnimBinding

/*下载图片之前的下载动画弹窗*/
class DownloadImagePopupWindow(context: Context) {
    private var mBinding: DownLoadAnimBinding? = null

    private val animPopupWindow: PopupWindow by lazy {
        mBinding = DownLoadAnimBinding.inflate(LayoutInflater.from(context))
        PopupWindow().apply {
            this.contentView = mBinding!!.root
            this.width = LayoutParams.WRAP_CONTENT
            this.height = LayoutParams.WRAP_CONTENT
        }
    }

    fun show(view: View) {
        animPopupWindow.showAtLocation(view, Gravity.CENTER, 0, 0)
    }

    fun hide() {
        mBinding!!.downLoadAnim.addAnimatorListener(object : Animator.AnimatorListener {
            override fun onAnimationStart(animation: Animator) {}
            override fun onAnimationCancel(animation: Animator) {}
            override fun onAnimationRepeat(animation: Animator) {}
            override fun onAnimationEnd(animation: Animator) {
                animPopupWindow.dismiss()
            }
        })
    }
}
